package com.imgo.max.experience.service;

import com.imgo.max.common.dto.ResultDO;
import com.imgo.max.common.log.CustomLogger;
import com.imgo.max.common.log.CustomLoggerFactory;
import com.imgo.max.common.result.ModuleResultCode;
import com.imgo.max.common.util.LoggerUtils;
import com.imgo.max.experience.dto.UserExperienceDto;
import com.imgo.max.experience.dto.UserExperienceMappingDto;
import com.imgo.max.experience.mapping.UserExperienceMapping;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 普通用户经验服务接口
 *
 * @author perter.Y
 */
@Component
public class UserExperienceServiceImpl implements UserExperienceService {

    private static final CustomLogger logger = CustomLoggerFactory.getLogger(UserExperienceServiceImpl.class);

    @Resource
    UserExperienceHandler userExperienceHandler;


    /**
     * 获取用户经验
     *
     * @param uid 用户ID
     * @return
     */
    @Override
    public ResultDO<UserExperienceDto> getUserExperience(String uid) {
        return userExperienceHandler.getUserExperience(uid);
    }


    /**
     * 获取用户经验
     *
     * @param uids 用户ID
     * @return
     */
    @Override
    public ResultDO<List<UserExperienceDto>> getUsersExperience(List<String> uids) {
        return userExperienceHandler.getUsersExperience(uids);
    }

    @Override
    public ResultDO<Integer> getUserLevel(String uid) {
        ResultDO<Integer> result = userExperienceHandler.getUserLevel(uid);
        return result;
    }

    @Override
    public ResultDO<List<UserExperienceMappingDto>> getExprMappingList() {
        ResultDO<List<UserExperienceMappingDto>> resultDO = ResultDO.buildResult();
        try {
            List<UserExperienceMapping> mappings = userExperienceHandler.getExprMappingList();
            if (CollectionUtils.isEmpty(mappings)) {
                return new ResultDO<>();
            }
            List<UserExperienceMappingDto> dtos = new ArrayList<>();
            for (UserExperienceMapping mapping : mappings) {
                UserExperienceMappingDto dto = new UserExperienceMappingDto();
                dto.setId(mapping.getId());
                dto.setDesc(mapping.getDesc());
                dto.setStartScore(mapping.getStartScore());
                dto.setEndScore(mapping.getEndScore());
                dto.setLevel(mapping.getLevel());
                dto.setLevelImage(mapping.getLevelImage());
                dtos.add(dto);
            }
            resultDO.setModule(dtos);
        } catch (Exception e) {
            LoggerUtils.errorAndResultDO(logger, e, ModuleResultCode.EXPERIENCE, resultDO);
        }
        return resultDO;
    }
}
